package com.shop.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.admin.dao.OrderTagDao;
import com.shop.admin.entity.OrderTag;
import com.shop.admin.service.OrderTagService;
import com.shop.admin.vo.TagListVo;
import com.shop.admin.vo.UpdateTagListVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderTagServiceImpl extends ServiceImpl<OrderTagDao, OrderTag> implements OrderTagService {
    @Override
    public List<TagListVo> selectTagList(Long orderId) {
        List<OrderTag> orderTags = this.baseMapper.selectList(new LambdaQueryWrapper<OrderTag>().eq(OrderTag::getOrderId, orderId));
        List<TagListVo> collect = orderTags.stream().map(item -> {
            TagListVo tagListVo = new TagListVo();
            BeanUtils.copyProperties(item, tagListVo);
            return tagListVo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    @Transactional
    public void updateTagList(UpdateTagListVo listVO) {
        List<UpdateTagListVo.TagList> vos = listVO.getVos();
        vos.stream().map(item->{
            OrderTag tag=new OrderTag();
            tag.setId(item.getId());
            tag.setTagKey(item.getTagKey());
            tag.setTagValue(item.getTagValue());
            tag.setOrderId(listVO.getOrderId());
           return this.saveOrUpdate(tag);
        }).collect(Collectors.toList());
    }

    @Override
    public void deleteTags(List<Long> asList) {
            this.removeByIds(asList);
    }
}
